package cn.com.customer.manager.ui.customer.add

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.FlowTagAdapter
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.base.BaseApplication.getContext
import cn.com.customer.manager.bean.*
import cn.com.customer.manager.ui.search.SearchActivity
import cn.com.customer.manager.utils.PictrueSelectUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.entity.LocalMedia
import com.xuexiang.xhttp2.subsciber.impl.IProgressLoader
import com.xuexiang.xhttp2.subsciber.impl.OnProgressCancelListener
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout.OnTagSelectListener
import com.xuexiang.xutil.display.ImageUtils
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.activity_add_customer.*
import kotlinx.android.synthetic.main.activity_add_task.*
import kotlinx.android.synthetic.main.layout_base_toolbar.*

class AddCustomerActivity : BaseActivity(), AddCustomerContract.View {
    var mPresenter: AddCustomerContract.Presenter = AddCustomerPresenter(this)
    var picturePath: String = ""
    private var mSelectList: List<LocalMedia> = mutableListOf()
    private var tags: MutableList<Tag> = mutableListOf()
    private var groups: MutableList<Type> = mutableListOf()
    override fun getLayoutId(): Int = R.layout.activity_add_customer

    override fun initView(savedInstanceState: Bundle?) {
        toolbar_title.text = getString(R.string.add_customer)
        mPresenter.start()
        add_customer_photo.setOnClickListener {
            PictrueSelectUtils.getPictureSelector(this)
                    .selectionMedia(mSelectList)
                    .maxSelectNum(1)
                    .selectionMode(PictureConfig.SINGLE)
                    .forResult(PictureConfig.CHOOSE_REQUEST)
        }
        add_customer_submit.setOnClickListener {
            submit()
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    // 图片选择
                    mSelectList = PictureSelector.obtainMultipleResult(data)
                    picturePath = mSelectList[0].path
                    add_customer_photo.setImageBitmap(ImageUtils.getBitmap(picturePath))
                }
                else -> {
                }
            }
        }
    }

    private fun submit() {
        var name = add_customer_name_content.text.toString()
        var sex = add_customer_sex_content.selectedIndex.toString()
        var birthday = add_customer_count_content.text.toString()
        var email = add_customer_email_content.text.toString()
        var tel = add_customer_tel_content.text.toString()
        var rtel = add_customer_rtel_content.text.toString()
        var company: String = add_customer_company_content.text.toString()
        var address = add_customer_address_content.text.toString()
        var remark = add_customer_maker.text.toString()
        var fax = add_customer_fax_content.text.toString()
        var country = add_customer_country_content.text.toString()
        var province = add_customer_province_content.text.toString()
        var city = add_customer_city_content.text.toString()
        var position = add_customer_city_content.text.toString()
//        var params: MutableMap<String, String> = mutableMapOf()

//        params["name"] =name
//        params["sex"] =sex
//        params["birthday"] =birthday
//        params["email"] =email
//        params["mobile"] =tel
//        params["telephone"] =rtel
//        params["companyName"] =company
//        params["address"] =address
//        params["remark"] =remark
//        params["fax"] =fax
//        params["country"] =country
//        params["province"] =province
//        params["city"] =city
//        params["duty"] =position
        var tags1: MutableList<Tag2> = mutableListOf()
        var groups1: MutableList<Group1> = mutableListOf()
        add_customer_select.selectedIndexs?.forEach {
            tags1.add(Tag2(tags[it-1].code))
        }
        add_customer_group_select.selectedIndexs?.forEach {
            groups1.add(Group1(groups[it-1].code))
        }
        var addCustomer = AddCustomer(address, birthday, city, company, country, position, email, fax, groups1, tel, name, province, remark, sex, tags1, rtel)
        mPresenter.addCustomer(addCustomer, picturePath, object : IProgressLoader {
            override fun updateMessage(msg: String?) {
            }

            override fun showLoading() {
            }

            override fun dismissLoading() {
            }

            override fun setCancelable(flag: Boolean) {

            }

            override fun isLoading(): Boolean {
                return true
            }

            override fun setOnProgressCancelListener(listener: OnProgressCancelListener?) {
            }

        })
    }


    override fun setTags(tags: List<Tag>) {
        this.tags = tags as MutableList<Tag>;
        val tagAdapter = FlowTagAdapter(getContext())
        add_customer_select.adapter = tagAdapter
        add_customer_select.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI)
        add_customer_select.setOnTagSelectListener(OnTagSelectListener { parent, position, selectedList ->

        })
        tagAdapter.addTags(tags)
        tagAdapter.setSelectedPositions(2, 3, 4)
    }

    override fun setGroupList(data: GroupResult) {
        val groupAdapter = FlowTagAdapter(getContext())
        this.groups = data.groupList as MutableList<Type>
        var groups: MutableList<Tag> = mutableListOf()

        data.groupList.forEach {
            groups.add(Tag(it.code, "", it.remark, it.remark))
        }
        add_customer_group_select.adapter = groupAdapter
        add_customer_group_select.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI)
        add_customer_group_select.setOnTagSelectListener(OnTagSelectListener { parent, position, selectedList ->

        })
        groupAdapter.addTags(groups)
        groupAdapter.setSelectedPositions(1)
    }

    override fun addSuccess() {
        ToastUtils.toast("上传成功")
    }

    override fun setPresenter(presenter: AddCustomerContract.Presenter) {
        mPresenter = presenter
    }
}
